<template>
  <div class="page" v-loading="loading">
    <div style="margin-bottom: 15px;">
      <el-form :inline="true" :model="info" class="demo-form-inline">
        <el-form-item label="题目名称:">
          <el-input @keyup.enter.native="initPage" v-model="info.content" placeholder="请输入题目关键字搜索"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="initPage"><i class="el-icon-search"></i> 查询</el-button>
          <el-button type="warning" plain @click="close"><i class="el-icon-delete"></i> 清除</el-button>
          <el-button type="primary" plain @click="flag=true"><i class="el-icon-download"></i> 导入Excel文档</el-button>
          <el-button type="success" plain @click="downloadFile()"><i class="el-icon-upload2"></i> 下载模板</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div style="margin-bottom: 15px;">
      <el-table :data="answerList" border>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="唯一编号" prop="id" width="100"></el-table-column>
        <el-table-column label="药名" prop="name" width="100"></el-table-column>
        <el-table-column label="英文名" sortable prop="englishName" width="150"></el-table-column>
        <el-table-column label="分类名" width="100">
          <template slot-scope="scope">
            <span>{{scope.row.pharmacyType == null ? '暂未分类' : scope.row.pharmacyType}}</span>
          </template>
        </el-table-column>
        <el-table-column label="功能描述" prop="description" width="300"></el-table-column>
        <el-table-column label="状态" >
          <template slot-scope="scope">
            <div>
              <el-tag size="mini" v-if="scope.row.disabled == 0">正常 </el-tag>
              <el-tag v-else　type="danger" size="mini">已禁用</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <div>
              <el-button type="primary" size="mini" @click="updateItem(scope.row)"><i class="el-icon-edit"></i> 修改题目</el-button>
              <el-button type="danger" size="mini" @click="disable(scope.row)" v-if="scope.row.disabled == 0"><i class="el-icon-delete"></i> 禁用题目</el-button>
              <el-button type="success" size="mini" @click="disable(scope.row)" v-else><i class="el-icon-circle-plus-outline"></i> 启用题目</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="display: flex;justify-content: flex-end;align-items: center;">
      <el-pagination :total="total" :page-size="info.limit" @current-change="currentChange" @size-change="handleSizeChange"
        background layout="sizes,prev,pager,next,jumper"></el-pagination>
    </div>
    <el-dialog title="修改题目信息" :visible.sync="show" width="30%" center >
      <el-form ref="form" :model="updateObj" label-width="100px">
        <el-form-item label="题目ID:" >
          <el-input v-model="updateObj.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="题目中文名称:">
          <el-input v-model="updateObj.name" placeholder="请输入题目中文名称"></el-input>
        </el-form-item>
        <el-form-item label="题目英文名称:">
          <el-input v-model="updateObj.englishName" placeholder="请输入题目英文名称"></el-input>
        </el-form-item>
        <el-form-item label="题目描述:">
          <el-input v-model="updateObj.description" placeholder="请输入题目描述"></el-input>
        </el-form-item>
        <el-form-item label="题目创建时间:">
          <el-input v-model="updateObj.createTime" disabled></el-input>
        </el-form-item>
        <el-form-item label="题目状态:"><el-tag type="mini">正常</el-tag></el-form-item>
        <el-form-item label="题目类型:">
              <el-input v-model="updateObj.pharmacyType" placeholder="请输入药品的分类"></el-input>
        </el-form-item>
        <el-form-item style="padding-left: 30px;">
          <el-button type="info" @click="show = false">取 消</el-button>
          <el-button type="primary" @click="updateSubmit()">提 交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="flag" center width="40%">
      <div slot="title">导入题目信息</div>
      <div style="padding: 10px 50px 0;">
        <el-form label-width="80px" ref="form">
          <el-form-item prop="classId" label="题目分类">
            <el-cascader
              v-model="value"
              :options="category"
              :props="{ expandTrigger: 'hover' }"
              @change="handleChange"></el-cascader>
          </el-form-item>
          <el-form-item label="学生信息">
            <el-upload class="upload-demo" drag :data="importInfo"
                       :action="$api + '/barrier/upload_file'" :auto-upload="false" ref="upload" >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button @click="flag = false" style="background-color: rgb(240,240,240);width: 120px;">取 消</el-button>
        <el-button type="primary" @click="uploadFile" style="width: 120px;">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    download
  } from "@/utils/util";
  export default {
    name: "manage-answer",
    data() {
      return {
        info: {
          content: "",
          page: 1,
          limit: 10,
          isAll:"all"
        },
        total: 0,
        answerList: [],
        flag: false,
        updateObj:{
          categoryId: 0,
          createTime: "",
          description: "",
          englishName: "",
          id: 0,
          isDel: 0,
          name: "",
          pharmacyType: 0,
          type:'',
        },
        show:false,
        loading:false,
        importInfo:{
          type: 0
        },
        category:[],
        value:''
      }
    },
    methods: {
      handleChange(item){
        this.importInfo.type = item[item.length - 1];
      },
      initPage() {
        this.loading = true
        const thizz = this;
        this.$get("/barrier/info_list", this.info).then(res => {
          if (res.code == 200) {
            this.answerList = res.data.data;
            this.total = res.data.count;
          }
          setTimeout(_=>{
            thizz.loading = false
          },300)
        })
        this.getCategory();
      },
      handleSizeChange(size){
        this.info.limit = size
        this.initPage()
      },
      currentChange(page){
        this.info.page = page
        this.initPage()
      },
      close() {
        this.info = {
          categoryId: '',
          name: "",
          page: 1,
          limit: 10
        }
      },
      getCategory(){
        this.category = [];
        this.$get("/category/list").then(result => {
          if(result.code == 200) {
            result.data.forEach(item => {
                let temp = {
                  value: item.id,
                  label: item.typeName,
                }
                let list = [];
                if(item.subCategoryList.length != 0){
                   item.subCategoryList.forEach(ele => { list.push({value:ele.id, label: ele.typeName});})
                  temp.children = list;
                }
                this.category.push(temp);
            })
          }
        })
      },
      updateItem(item){
         let abc = this.updateObj;
         abc.pharmacyType=item.pharmacyType;abc.name=item.name;abc.id=item.id;abc.englishName=item.englishName;abc.score=item.score;
         abc.categoryId=item.categoryId;abc.createTime=item.createTime;abc.description=item.description;
         abc.isDel=item.isDel;abc.type = item.type;
         this.show = true;
      },
      updateSubmit(){
        this.updateObj.createTime = "";
        this.$post("/barrier/update_barrier_data", this.updateObj).then(result => {
          if(result.code == 200){
              this.$message.success(result.data);
              this.initPage();
          }else{
            this.$message.warning(result.data);
          }
          this.show = false;
        })
      },
      disable(data){
        let message = "禁用该题目后该题目不会再出现在已有的关卡中，是否禁用该题目？";
        if(data.disabled == 1){
          message = "是否解封此题目？"
        }
        this.$confirm(message).then(abc => {
          this.$post("/barrier/disable", {id:data.id, changeMethod: 1}).then(res => {
            if(res.code == 200){
              this.$message.success(res.data);
              this.initPage();
            }else
              this.$message.warning(res.data);
          })
        })
      },
      downloadFile(){
        download('题目信息', "/static/res/excelTemplate.xlsx")
      },
      uploadFile(){
        this.$confirm("上传后就不能撤回了哦，是否确认上传？").then(abc => {
          this.$refs.upload.submit();
          this.flag = false;
          this.$message.success("上传成功，请等待结果");
          this.initPage();
        })
      }
    },
    created() {
      this.initPage()
    },
  }
</script>

<style lang="scss" scoped>
  .page {
    border-radius: 8px;
    background-color: #fff;
    min-height: calc(100vh - 105px);
    box-sizing: border-box;
    overflow-y: auto;
    padding: 15px;
  }
</style>
